/* eslint-disable */

/**
 * jQuery object
 * @external jQuery
 * @see {@link http://api.jquery.com/jQuery/}
 */

const decompressBtn = $('#lz-button');
const output = document.querySelector('.lz-output');
$(decompressBtn).click(handleInputChange);

const compressQueryParams = (str) => (typeof str === 'object'
    ? LZString.compressToEncodedURIComponent(JSON.stringify(str))
    : LZString.compressToEncodedURIComponent(str));

const getQueryParams = (q) => LZString.decompressFromEncodedURIComponent(decodeURIComponent(q));

function setOutput(dataStr) {
  output.innerHTML = dataStr;
}

function setJSONOutput(dataStr) {
  setOutput('');

  let result;
  try {
    result = JSON.parse(dataStr);
  } catch (e) {
    setOutput(e.message);
    return;
  }

  if (!result) {
    setOutput('not a valid lz-string string');
    return;
  }

  const tree = jsonTree.create(result, output);
  tree.expand();
}

function handleInputChange() {
  const input = $('#lz-text');
  
  const decompressStr = input.val();

  if (decompressStr === '') {
    setOutput('nothing')
    return;
  }

  const ret = getQueryParams(decompressStr);
  setJSONOutput(ret);
}
